package com.syedu.day15;

/**
 * description：Java实现多线程的方式之一:
 * 写一个类,继承java.lang.Thread类并且重写run()方法
 *
 * @author Administrator
 * @date 2022/3/31 14:25
 */
public class ThreadDemo1 extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            //getName()用来获取线程的名称
            System.out.println(getName() + " : " + i);

            //添加线程睡眠延时,放置方法打印太快
            try {
                //线程睡眠
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class TestThreadDemo1 {
    public static void main(String[] args) {
        //创建线程
        Thread t1 = new ThreadDemo1();
        Thread t2 = new ThreadDemo1();
        //启动线程
        t1.start();
        t2.start();

        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>");

        //匿名方式创建线程
        /*new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    System.out.println(getName() + " : " + i);
                    try {
                        //线程睡眠
                        Thread.sleep(1500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();*/
    }
}